package com.ai.learning.framework.core.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 错误码枚举 - 统一管理所有系统错误码
 * 与Python Django后端保持错误码一致性
 */
@Getter
@AllArgsConstructor
public enum ErrorCode {
    
    // 系统级错误 1xxx
    SUCCESS(200, "操作成功"),
    SYSTEM_ERROR(1001, "系统内部错误"),
    SERVICE_UNAVAILABLE(1002, "服务暂不可用"),
    REQUEST_TIMEOUT(1003, "请求超时"),
    
    // 参数校验错误 2xxx  
    VALIDATION_FAILED(2001, "参数校验失败"),
    INVALID_PARAMETER(2002, "参数无效"),
    MISSING_PARAMETER(2003, "缺少必要参数"),
    INVALID_ID_FORMAT(2004, "ID格式无效"),
    
    // 业务错误 3xxx
    BUSINESS_ERROR(3001, "业务处理失败"),
    DATA_NOT_FOUND(3002, "数据不存在"),
    DATA_ALREADY_EXISTS(3003, "数据已存在"),
    OPERATION_NOT_ALLOWED(3004, "操作不被允许"),
    
    // 认证授权错误 4xxx
    UNAUTHORIZED(4001, "未登录或登录已过期"),
    ACCESS_DENIED(4003, "访问被拒绝"),
    TOKEN_INVALID(4004, "Token无效"),
    TOKEN_EXPIRED(4005, "Token已过期"),
    
    // HTTP方法错误 5xxx
    METHOD_NOT_ALLOWED(5001, "不支持的请求方法"),
    MEDIA_TYPE_NOT_SUPPORTED(5002, "不支持的媒体类型"),
    
    // 与Django后端保持一致的业务错误码 6xxx
    USER_NOT_FOUND(6001, "用户不存在"),
    USER_ALREADY_EXISTS(6002, "用户已存在"),
    PASSWORD_INCORRECT(6003, "密码错误"),
    USER_LOCKED(6004, "用户已锁定"),
    
    AGENT_NOT_FOUND(6101, "智能体不存在"),
    AGENT_CREATE_FAILED(6102, "智能体创建失败"),
    AGENT_TRAINING_FAILED(6103, "智能体训练失败"),
    
    CONVERSATION_NOT_FOUND(6201, "对话不存在"),
    MESSAGE_SEND_FAILED(6202, "消息发送失败"),
    
    KNOWLEDGE_BASE_NOT_FOUND(6301, "知识库不存在"),
    DOCUMENT_UPLOAD_FAILED(6302, "文档上传失败"),
    DOCUMENT_PROCESS_FAILED(6303, "文档处理失败");
    
    private final int code;
    private final String message;
    
    /**
     * 根据错误码查找枚举
     */
    public static ErrorCode fromCode(int code) {
        for (ErrorCode errorCode : ErrorCode.values()) {
            if (errorCode.getCode() == code) {
                return errorCode;
            }
        }
        return SYSTEM_ERROR;
    }
}